package com.shishuo.cms.action.manage;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.shishuo.cms.Directory;
import com.shishuo.cms.dao.PictureDao;
import com.shishuo.cms.entity.Picture;
import com.shishuo.cms.entity.Product;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.service.ProductService;
import com.shishuo.cms.util.MediaUtils;

@RequestMapping("/manage/product")
@Controller
public class ManageProductAction extends ManageBaseAction {

	@Autowired
	private ProductService productService;
	@Autowired
	private PictureDao pictureDao;

	@RequestMapping(value = "/list.htm", method = RequestMethod.GET)
	public String list(ModelMap modelMap,
			@RequestParam(value = "folderId", defaultValue = "0") long folderId,
			@RequestParam(value = "p", defaultValue = "1") int p)
			throws Exception {
		PageVo<Product> pageVo = productService.getProductPage(p,folderId);
		modelMap.put("pageVo", pageVo);
		return "manage/product/list";
	}
	
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String details( 
			ModelMap modelMap) throws Exception {
		return "manage/product/add";
	}
	
	@ResponseBody
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public JsonVo<String> addReply(@RequestParam(value = "productName", required = false) String productName,
			@RequestParam(value = "price", required = false) double price,
			@RequestParam(value = "content", required = false) String content,
			MultipartHttpServletRequest request,
			ModelMap modelMap) {
		List<MultipartFile> file = request.getFiles("file");     
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(productName)) {
			json.getErrors().put("productName", "产品名称不能为空");
			json.setMsg("产品名称不能为空");
		}
		if (StringUtils.isBlank(content)) {
			json.getErrors().put("content", "产品详情不能为空");
			json.setMsg("产品详情不能为空");
		}
		if (file.size()==0) {
			json.getErrors().put("file", "产品图片不能为空");
			json.setMsg("产品图片不能为空");
		}else {
			for (MultipartFile singleFile : file) {
				if (!MediaUtils.isImageType(singleFile.getOriginalFilename())) {
					json.getErrors().put("file", "不是图片类型");
					json.setMsg("不是图片类型");
				}
			}
		}
		try {
			// 检测校验结果
			json.check();
			json.setResult(true);
			Product product=new Product();
			product.setProductName(productName);
			product.setPrice(price);
			product.setContent(content);
			if (file!=null) {
				List<Picture> pictures=new ArrayList<>();
				for (MultipartFile singleFile : file) {
					Picture picture=new Picture();
					picture.setUrl(MediaUtils.saveImage(singleFile, Picture.width,
							Picture.height));
					pictures.add(picture);
				}
				product.setPictureList(pictures);
				
			}
			productService.addProduct(product);
		} catch (Exception e) {
			json.setResult(false);
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping(value = "/details.htm", method = RequestMethod.GET)
	public String details(@RequestParam("productId") long productId,
			ModelMap modelMap) throws Exception {
		Product product = productService.getProductById(productId);
		List<FolderVo> folderVos=new ArrayList<>();
		List<FolderVo> childFolderVos=folderService.getFolderListByFatherId(
				Directory.PRODUCT.getType(), null);
		if (childFolderVos.size()==0) {
			folderVos.add(folderService.getFolderById(Directory.PRODUCT.getType()));
		}else {
			folderVos.addAll(childFolderVos);
		}
		modelMap.put("folderAll", folderVos);
		modelMap.put("product", product);
		return "manage/Product/update";
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public JsonVo<String> deleteFile(
			@RequestParam(value = "productId") long productId) {
		JsonVo<String> json = new JsonVo<String>();
		productService.deleteProduct(productId);
		json.setResult(true);
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deletePic.json", method = RequestMethod.POST)
	public JsonVo<String> deletePic(
			@RequestParam(value = "pictureId") long pictureId) {
		JsonVo<String> json = new JsonVo<String>();
		pictureDao.deletePicture(pictureId);
		json.setResult(true);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> status(
			@RequestParam(value = "productName", required = false) String productName,
			@RequestParam(value = "productId", required = false) long productId,
			@RequestParam(value = "price", required = false) double price,
			@RequestParam(value = "content", required = false) String content,
			MultipartHttpServletRequest request,
			ModelMap modelMap) {
		List<MultipartFile> file = request.getFiles("file");     
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(productName)) {
			json.getErrors().put("productName", "产品名称不能为空");
			json.setMsg("产品名称不能为空");
		}
		if (StringUtils.isBlank(content)) {
			json.getErrors().put("content", "产品详情不能为空");
			json.setMsg("产品详情不能为空");
		}
		if (file!=null&&file.size()>0){
			for (MultipartFile singleFile : file) {
				if (!MediaUtils.isImageType(singleFile.getOriginalFilename())) {
					json.getErrors().put("file", "不是图片类型");
					json.setMsg("不是图片类型");
				}
			}
		}
		try {
			// 检测校验结果
			json.check();
			json.setResult(true);
			Product product=new Product();
			product.setProductId(productId);
			product.setProductName(productName);
			product.setPrice(price);
			product.setContent(content);
			if (file!=null) {
				List<Picture> pictures=new ArrayList<>();
				for (MultipartFile singleFile : file) {
					Picture picture=new Picture();
					picture.setUrl(MediaUtils.saveImage(singleFile, Picture.width,
							Picture.height));
					pictures.add(picture);
				}
				product.setPictureList(pictures);
				
			}
			productService.updateProduct(product);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	} 

}
